<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Menashop
 * @package    Menashop_Content
 * @subpackage Languages
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The SAX handler for languages
 * 
 * @category   Menashop
 * @package    Menashop_Content
 * @subpackage Languages
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Menashop_Content_Languages
{
    /**
     * The short codes
     * @var array
     */
    protected $_shortCodes;

    /**
     * The data model for language
     * @var object 
     */
    protected $_languageModel;

    /**
     * The counter for languages
     * @var integer 
     */
    protected $_totalLanguages = 0;

    /**
     * The counter for imported languages
     * @var integer 
     */
    protected $_totalLanguagesImported = 0;

    /**
     * Initialize the SAX handler for languages
     */
    public function __construct($shortCodes)
    {
        $this->_languageModel = new Admin_Model_Language();
        $this->_shortCodes = explode(',', $shortCodes);
    }

    /**
     * Process the start of an element in XML
     * @param obejct $parser
     * @param string $tag
     * @param array $attributes 
     */
    public function startElement($parser, $tag, $attributes)
    {
        if ('Language' == $tag) {
            if (in_array($attributes['ShortCode'], $this->_shortCodes)) {
                $language = array();
                $language['id'] = $attributes['ID'];
                $language['code'] = $attributes['Code'];
                $language['short_code'] = $attributes['ShortCode'];
                // save language
                $this->_totalLanguagesImported++;
                $this->_languageModel->save($language);
                $this->_totalLanguages++;
            }
        }
    }

    /**
     * Process the end of an element in XML
     * @param object $parser
     * @param string $tag 
     */
    public function endElement($parser, $tag)
    {
        
    }

    /**
     * Process characters in XML
     * @param object $parser
     * @param string $data 
     */
    public function characterData($parser, $data)
    {
        
    }

    /**
     * Get the report about a parsing
     * @return string 
     */
    public function getReport()
    {
        $report = "$this->_totalLanguages languages in XML,";
        $report .= " $this->_totalLanguagesImported imported";
        return $report;
    }

}

